package com.csweimei.rentmanagementtreasure.pop;

import android.content.Context;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.lxj.xpopup.core.BottomPopupView;
import com.weigan.loopview.LoopView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopWheelBase extends BottomPopupView {

    private Context mContext;
    private Click mClick;
    private TextView cancel,sure;
    private List<String> mList1 = new ArrayList<>();
    private List<String> mList2 = new ArrayList<>();

    private List<String> mList3 = new ArrayList<>();

    private List<String> mList4 = new ArrayList<>();

    LoopView loopView1,loopView2,loopView3,loopView4;
    private String sel,sel2,sel3,sel4;
    private int mLoopNum = 1;

    public interface Click{
        void click(String s);
    }




    public PopWheelBase(@NonNull Context context,int loopNum,  Click click) {
        super(context);
        this.mClick = click;
        mLoopNum = loopNum;

    }

    public void setList1(List<String> list){
        mList1.clear();
        mList1.addAll(list);
        sel = mList1.get(0);
    }

    public void setList2(List<String> list2){
        mList2.clear();
        mList2.addAll(list2);
        sel2 = mList2.get(0);
    }

    public void setList3(List<String> list){
        mList3.clear();
        mList3.addAll(list);
        sel3 = mList3.get(0);
    }

    public void setList4(List<String> list){
        mList4.clear();
        mList4.addAll(list);
        sel4 = mList4.get(0);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_flood_wheel;
    }

    @Override
    protected void onCreate() {
        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);
        loopView1 = findViewById(R.id.loop_1);
        loopView2 = findViewById(R.id.loop_2);
        loopView3 = findViewById(R.id.loop_3);
        loopView4 = findViewById(R.id.loop_4);


        cancel.setOnClickListener(view ->{
            dismiss();
        });
        sure.setOnClickListener(view ->{
            if (ValidateUtils.isNotEmptyString(sel)){
                switch (mLoopNum){
                    case 1:
                        mClick.click(sel);
                        break;
                    case 2:
                        mClick.click(sel+"-"+sel2);
                        break;
                    case 3:
                        mClick.click(sel+"-"+sel2+"-"+sel3);
                        break;
                    case 4:
                        mClick.click(sel+"-"+sel2+"-"+sel3+"-"+sel4);
                        break;
                }
                dismiss();
            }else {
                ToastUtils.showShort("请选择");
            }

        });

        switch (mLoopNum){
            case 1:
                initLoopView1(loopView1,mList1);
                loopView1.setVisibility(VISIBLE);
                loopView2.setVisibility(GONE);
                loopView3.setVisibility(GONE);
                loopView4.setVisibility(GONE);
                break;
            case 2:
                initLoopView1(loopView1,mList1);
                initLoopView2(loopView2,mList2);
                loopView1.setVisibility(VISIBLE);
                loopView2.setVisibility(VISIBLE);
                loopView3.setVisibility(GONE);
                loopView4.setVisibility(GONE);
                break;
            case 3:
                initLoopView1(loopView1,mList1);
                initLoopView2(loopView2,mList2);
                initLoopView3(loopView3,mList3);
                loopView1.setVisibility(VISIBLE);
                loopView2.setVisibility(VISIBLE);
                loopView3.setVisibility(VISIBLE);
                loopView4.setVisibility(GONE);
                break;
            case 4:
                initLoopView1(loopView1,mList1);
                initLoopView2(loopView2,mList2);
                initLoopView3(loopView3,mList3);
                initLoopView4(loopView4,mList4);
                loopView1.setVisibility(VISIBLE);
                loopView2.setVisibility(VISIBLE);
                loopView3.setVisibility(VISIBLE);
                loopView4.setVisibility(VISIBLE);
                break;
        }

    }

    private void initLoopView1(LoopView loopView,List<String> mList) {
        //滚动监听
        loopView.setListener(index -> {
            sel = mList.get(index);
        });
        //设置原始数据
        loopView.setItems(mList);
        //设置初始位置
        loopView.setInitPosition(0);

    }

    private void initLoopView2(LoopView loopView,List<String> mList) {
        //滚动监听
        loopView.setListener(index -> {
            sel2 = mList.get(index);
        });
        //设置原始数据
        loopView.setItems(mList);
        //设置初始位置
        loopView.setInitPosition(0);

    }


    private void initLoopView3(LoopView loopView,List<String> mList) {
        //滚动监听
        loopView.setListener(index -> {
            sel3 = mList.get(index);
        });
        //设置原始数据
        loopView.setItems(mList);
        //设置初始位置
        loopView.setInitPosition(0);

    }

    private void initLoopView4(LoopView loopView,List<String> mList) {
        //滚动监听
        loopView.setListener(index -> {
            sel4 = mList.get(index);
        });
        //设置原始数据
        loopView.setItems(mList);
        //设置初始位置
        loopView.setInitPosition(0);

    }

}
